import request from "@/config/axios/service";
const prefix = "javascript:";
// 对输入字符进行转义处理
export const translateStr = (target) => {
  if (typeof target === "string") {
    if (target.startsWith(prefix)) {
      const funcStr = target.split(prefix)[1];
      let result;
      try {
        result = new Function(`${funcStr}`)();
      } catch (error) {
        console.log(error);
      }
      return result;
    } else {
      return target;
    }
  }
  for (const key in target) {
    if (Object.prototype.hasOwnProperty.call(target, key)) {
      const subTarget = target[key];
      target[key] = translateStr(subTarget);
    }
  }
  return target;
};

/**
 * * 自定义请求
 * @param targetParams 当前组件参数
 * @param globalParams 全局参数
 */
export const customizeHttp = (targetParams, globalParams) => {
  if (!targetParams || !globalParams) {
    return;
  }
  // 全局
  const {
    // 全局请求源地址
    requestOriginUrl,
    // 全局请求内容
    requestParams: globalRequestParams,
  } = globalParams;

  // 目标组件（优先级 > 全局组件）
  const {
    // 请求地址
    requestUrl,
    // 普通 / sql
    requestContentType,
    // 获取数据的方式
    requestDataType,
    // 请求方式 get/post/del/put/patch
    requestHttpType,
    // 请求体类型 none / form-data / x-www-form-urlencoded / json /xml
    requestParamsBodyType,
    // SQL 请求对象
    requestSQLContent,
    // 请求内容 params / cookie / header / body: 同 requestParamsBodyType
    requestParams: targetRequestParams,
  } = targetParams;

  // 静态排除
  if (requestDataType === "static") return;

  if (!requestUrl) {
    return;
  }

  // 处理头部
  let headers = {
    ...globalRequestParams.Header,
    ...targetRequestParams.Header,
  };
  headers = translateStr(headers);

  // data 参数
  let data = {};
  // params 参数
  let params = { ...targetRequestParams.Params };
  params = translateStr(params);
  // form 类型处理
  let formData = new FormData();
  // 类型处理

  switch (requestParamsBodyType) {
    case "none":
      break;

    case "json":
      headers["Content-Type"] = "application/json;charset=UTF-8";
      //json对象也能使用'javasctipt:'来动态拼接参数
      data = translateStr(targetRequestParams.Body["json"]);
      if (typeof data === "string") data = JSON.parse(data);
      // json 赋值给 data
      break;

    case "xml":
      headers["Content-Type"] = "application/xml;charset=UTF-8";
      // xml 字符串赋值给 data
      data = translateStr(targetRequestParams.Body["xml"]);
      break;

    case "x-www-form-urlencoded": {
      headers["Content-Type"] =
        "application/x-www-form-urlencoded;charset=UTF-8";
      const bodyFormData = targetRequestParams.Body["x-www-form-urlencoded"];
      for (const i in bodyFormData)
        formData.set(i, translateStr(bodyFormData[i]));
      // FormData 赋值给 data
      data = formData;
      break;
    }

    case "form-data": {
      headers["Content-Type"] = "multipart/form-data;charset=UTF-8";
      const bodyFormUrlencoded = targetRequestParams.Body["form-data"];
      for (const i in bodyFormUrlencoded) {
        formData.set(i, translateStr(bodyFormUrlencoded[i]));
      }
      // FormData 赋值给 data
      data = formData;
      break;
    }
  }

  // sql 处理
  // if (requestContentType === RequestContentTypeEnum.SQL) {
  //   headers["Content-Type"] = "application/json;charset=UTF-8"
  //   data = requestSQLContent
  // }

  try {
    const url = new Function(
      "return `" + `${requestOriginUrl}${requestUrl}`.trim() + "`"
    )();
    return request({
      url,
      method: requestHttpType,
      data,
      params,
      headers,
    });
  } catch (error) {
    console.log(error);
  }
};
